import { message } from "@/utils/message";
import { queryZjyctjMonthList, sendMainZjyctjMonth } from "@/api/sjtj";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, onMounted } from "vue";

export function useRole() {
  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "统计期别",
      prop: "tjqb",
      minWidth: 100
    },
    {
      label: "地区",
      prop: "area",
      minWidth: 100
    },
    {
      label: "登记机关",
      prop: "organName",
      minWidth: 100
    },
    {
      label: "本期经营主体新增数量",
      prop: "xzzs",
      minWidth: 100
    },
    {
      label: "同比",
      prop: "xzzsHb",
      minWidth: 100
    },
    {
      label: "本期经营主体退出数量",
      prop: "tczs",
      minWidth: 100
    },
    {
      label: "同比",
      prop: "tczsHb",
      minWidth: 100
    },
    {
      label: "本期企业新增数量",
      prop: "qyxzzs",
      minWidth: 100
    },
    {
      label: "同比",
      prop: "qyxzzsHb",
      minWidth: 100
    },
    {
      label: "本期企业退出数量",
      prop: "qytczs",
      minWidth: 100
    },
    {
      label: "同比",
      prop: "qytczsHb",
      minWidth: 100
    },
    {
      label: "本期个体新增数量",
      prop: "gtxzzs",
      minWidth: 100
    },
    {
      label: "同比",
      prop: "gtxzzsHb",
      minWidth: 100
    },
    {
      label: "本期个体退出数量",
      prop: "gttczs",
      minWidth: 100
    },
    {
      label: "同比",
      prop: "gttczsHb",
      minWidth: 100
    }
  ];

  async function onSearch() {
    loading.value = true;
    queryZjyctjMonthList()
      .then(res => {
        if (res.success) {
          dataList.value = res.data.list;
          pagination.total = res.data.total;
          loading.value = false;
        } else {
          message("查询数据异常:" + res.errMsg, { type: "error" });
        }
      })
      .catch(error => {
        message("查询数据异常:" + JSON.stringify(error), { type: "error" });
      });
  }

  async function sendMail() {
    sendMainZjyctjMonth()
      .then(res => {
        if (res.success) {
          message("发送邮件成功", { type: "success" });
        } else {
          message("发送邮件失败:" + res.errMsg, { type: "error" });
        }
      })
      .catch(error => {
        message("发送邮件失败:" + JSON.stringify(error), { type: "error" });
      });
  }

  onMounted(() => {
    onSearch();
  });

  return {
    loading,
    columns,
    dataList,
    onSearch,
    sendMail
  };
}
